package service.impl;

import global.Constants;
import global.security.SessionUtils;

import java.sql.Timestamp;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import service.NoteCategoryService;
import service.NoteService;
import dao.NoteCategoryDAO;
import domain.Note;
import domain.NoteCategory;

/**
 * 笔记分类Service实现类.
 * <p>
 * 创建日期：2010-07-01<br>
 * 创建人：Xiyt<br>
 * 修改日期：<br>
 * 修改人：<br>
 * 修改内容：<br>
 * 
 * @author Xiyt
 * @version 1.0
 */
public class NoteCategoryServiceImpl implements NoteCategoryService {

	/**
	 * 分类Dao
	 * */
	private NoteCategoryDAO noteCategoryDao = null;
	/**
	 * 笔记Service
	 * */
	private NoteService noteService = null;

	/**
	 * @param noteCategoryDao the noteCategoryDao to set
	 */
	public void setNoteCategoryDao(NoteCategoryDAO noteCategoryDao) {
		this.noteCategoryDao = noteCategoryDao;
	}

	/**
	 * 设定笔记Service
	 * @param noteService
	 *            the noteService to set
	 */
	public void setNoteService(NoteService noteService) {
		this.noteService = noteService;
	}
	
	/**
	 * 查询分类数量
	 * @param paramCategory 分类
	 * @return int 分类数量
	 */
	public int countByCriteria(NoteCategory paramNoteCategory) {
		paramNoteCategory = getConditions(paramNoteCategory);
		int count = noteCategoryDao.countByCriteria(paramNoteCategory);
		return count;
	}

	/**
	 * 查询分类列表不带分页
	 * @param paramCategory 分类
	 * @return List 分类列表
	 */
	public List<NoteCategory> selectByCriteria(NoteCategory paramNoteCategory) {
		// 查询分类列表不带分页
		paramNoteCategory = getConditions(paramNoteCategory);
		List<NoteCategory> categoryList = noteCategoryDao.selectByCriteria(paramNoteCategory);
		if (categoryList != null && categoryList.size() != 0) {
			return categoryList;
		} else {
			return null;
		}
	}

	/**
	 * 查询分类树
	 * @return JSONArray 分类树
	 */
	public JSONArray selectNoteCategoryForTree() {
		JSONArray res = new JSONArray();
		// 设定条件
		NoteCategory paramCategory = new NoteCategory();
		paramCategory.setCreateUser(SessionUtils.getCurrentUserId());
		// 查询分类列表不带分页
		List<NoteCategory> categoryList = noteCategoryDao.selectCategoryForTree(paramCategory);
		res = getCategoryTreeFromList(categoryList);
		return res;
	}

	/**
	 * 查询分类列表带分页
	 * @param paramCategory 分类
	 * @return List 分类列表
	 */
	public List<NoteCategory> selectByCriteriaForPaging(NoteCategory paramNoteCategory) {
		// 查询分类列表带分页
		paramNoteCategory = getConditions(paramNoteCategory);
		List<NoteCategory> categoryList = noteCategoryDao.selectByCriteriaForPaging(paramNoteCategory, paramNoteCategory.getStart(), paramNoteCategory.getLimit());
		if (categoryList != null && categoryList.size() != 0) {
			return categoryList;
		} else {
			return null;
		}
	}
	
	/**
	 * 添加分类
	 * @param paramCategory 分类
	 */
	public void insert(NoteCategory paramNoteCategory) {
		// 删除区分：未删除
		paramNoteCategory.setDelflag(Constants.DEL_FLAG_1);
		// 系统时间
		Timestamp sysdate = new Timestamp(System.currentTimeMillis());
		paramNoteCategory.setCreateUser(SessionUtils.getCurrentUserId());
		paramNoteCategory.setCreateTime(sysdate);
		paramNoteCategory.setUpdateUser(SessionUtils.getCurrentUserId());
		paramNoteCategory.setUpdateTime(sysdate);
		noteCategoryDao.insert(paramNoteCategory);
	}
	
	
	/**
	 * 修改分类
	 * @param paramCategory 分类
	 */
	public void update(NoteCategory paramNoteCategory) {
		// 系统时间
		Timestamp sysdate = new Timestamp(System.currentTimeMillis());
		paramNoteCategory.setUpdateUser(SessionUtils.getCurrentUserId());
		paramNoteCategory.setUpdateTime(sysdate);
		noteCategoryDao.updateByPrimaryKey(paramNoteCategory);
	}
	
	
	/**
	 * 删除分类
	 * @param categoryId 分类ID
	 */
	public void delete(int categoryId) {
		// 删除笔记分类
		noteCategoryDao.deleteByPrimaryKey(categoryId);
	}
	
	/**
	 * 返回笔记树
	 * @param categoryList
	 * @return
	 */
	private JSONArray getCategoryTreeFromList(List<NoteCategory> categoryList) {
		JSONArray results = new JSONArray();
		// 所有笔记
		Note paramNote = new Note();
		paramNote.setDelflag(Constants.DEL_FLAG_1);
		int noteCount = noteService.countByCriteria(paramNote);
		JSONObject allCategory = new JSONObject();
		allCategory.put("id", "allCategory");
		allCategory.put("text", "所有笔记 ("+noteCount+")");
		allCategory.put("leaf", true);
		allCategory.put("attributes", "{textNoCount:'所有笔记'}");
		results.add(allCategory);
		// 笔记分类
		if (null != categoryList) {
			JSONArray children = new JSONArray();
			NoteCategory temp = new NoteCategory();
			for (int i = 0; i < categoryList.size(); i++) {
				temp = categoryList.get(i);
				JSONObject categoryNode = new JSONObject();
				categoryNode.put("id", temp.getCategoryId());
				categoryNode.put("text", temp.getCategoryName()+"("+temp.getNoteCount()+")");
				categoryNode.put("leaf", true);
				categoryNode.put("attributes", "{textNoCount:'"+temp.getCategoryName()+"'}");
				children.add(categoryNode);
			}
			JSONObject category = new JSONObject();
			category.put("id", "0");
			category.put("text", "笔记分类");
			category.put("children", children);

			results.add(category);
		}
		// 回收站
		JSONObject rubbish = new JSONObject();
		rubbish.put("id", "rubbish");
		rubbish.put("text", "回收站");
		rubbish.put("leaf", true);
		results.add(rubbish);

		return results;
	}
	
	/**
	 * 设置公共参数
	 * @param paramNoteCategory 参数对象
	 */
	private NoteCategory getConditions(NoteCategory paramNoteCategory){
		paramNoteCategory.setCreateUser(SessionUtils.getCurrentUserId());// 当前用户
		return paramNoteCategory;
	}
}
